tests/build.rs: remove empty components from LD_LIBRARY_PATH
authorNODA, Kai <nodakai@gmail.com>
Wed, 19 Jul 2017 13:59:50 +0000 (21:59 +0800)
committerNODA, Kai <nodakai@gmail.com>
Fri, 21 Jul 2017 08:16:16 +0000 (16:16 +0800)
This is to amend PR #4278

The tests intended to ensure Cargo wouldn't mistakenly add empty
components to LD_LIBRARY_PATH (or its equivalents) but they could fail
when the test runner already had empty components in LD_LIBRARY_PATH
from their environment.

Signed-off-by: NODA, Kai <nodakai@gmail.com>
tests/build.rs

index 59a7e451777d963e8865fe4182f6172e9ee2c310..f3e9445f8e39043bc5902409c598d1458ae81263 100644 (file)
@@ -8,7 +8,7 @@ use std::fs::{self, File};
 use std::io::prelude::*;
 
 use cargo::util::paths::dylib_path_envvar;
-use cargo::util::process;
+use cargo::util::{process, ProcessBuilder};
 use cargotest::{is_nightly, rustc_host, sleep_ms};
 use cargotest::support::paths::{CargoPathExt,root};
 use cargotest::support::{ProjectBuilder};
@@ -985,6 +985,18 @@ fn crate_authors_env_vars() {
                 execs().with_status(0));
 }
 
+// The tester may already have LD_LIBRARY_PATH=::/foo/bar which leads to a false positive error
+fn setenv_for_removing_empty_component(mut p: ProcessBuilder) -> ProcessBuilder {
+    let v = dylib_path_envvar();
+    if let Ok(search_path) = env::var(v) {
+        let new_search_path =
+            env::join_paths(env::split_paths(&search_path).filter(|e| !e.as_os_str().is_empty()))
+                .expect("join_paths");
+        p.env(v, new_search_path); // build_command() will override LD_LIBRARY_PATH accordingly
+    }
+    p
+}
+
 // Regression test for #4277
 #[test]
 fn crate_library_path_env_var() {
@@ -1004,7 +1016,8 @@ fn crate_library_path_env_var() {
             }}
         "##, dylib_path_envvar()));
 
-    assert_that(p.cargo_process("run"), execs().with_status(0));
+    assert_that(setenv_for_removing_empty_component(p.cargo_process("run")),
+                execs().with_status(0));
 }
 
 // Regression test for #4277
@@ -1023,7 +1036,8 @@ fn build_with_fake_libc_not_loading() {
         .file("src/lib.rs", r#" "#)
         .file("libc.so.6", r#""#);
 
-    assert_that(p.cargo_process("build"), execs().with_status(0));
+    assert_that(setenv_for_removing_empty_component(p.cargo_process("build")),
+                execs().with_status(0));
 }
 
 // this is testing that src/<pkg-name>.rs still works (for now)